package com.syyo.gulimall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.gulimall.order.domain.entity.OrderSettingEntity;
import com.syyo.gulimall.order.domain.req.OrderSettingReq;
import com.syyo.gulimall.order.mapper.OrderSettingMapper;
import com.syyo.gulimall.order.service.OrderSettingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 订单配置信息
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 10:01:14
 */
@Service
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSettingEntity> implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;


    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(OrderSettingReq req) {
        OrderSettingEntity entity = new OrderSettingEntity();
        // 添加创建时间和更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = orderSettingMapper.insert(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10001.getCode(), ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = orderSettingMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(OrderSettingReq req) {
        OrderSettingEntity entity = new OrderSettingEntity();
        // 添加更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = orderSettingMapper.updateById(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public OrderSettingEntity findOne(Integer id) {
        return orderSettingMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<OrderSettingEntity> findAll(Integer pageNum, Integer pageSize, OrderSettingReq req) {
        Page<OrderSettingEntity> teacherPage = new Page<OrderSettingEntity>(pageNum, pageSize);
        QueryWrapper<OrderSettingEntity> wrapper = new QueryWrapper<>();
        return orderSettingMapper.selectPage(teacherPage, wrapper);
    }

}